import { createRouter, createWebHistory } from 'vue-router'
import VueCookie from 'vue-cookie'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/index',
      name: 'Index',
      component: () => import('@/views'),
	  children: [
		  {
			  path: '/index',
			  name: 'IndexShow',
			  component: () => import('@/views/show')
		  },
		  {
			  path: '/interview',
			  name: 'Interview',
			  component: () => import('@/views/ivi/interview')
		  },
		  {
			  path: '/history',
			  name: 'History',
			  component: () => import('@/views/ivi/history')
		  },
		  {
			  path:'/admin',
			  name:'AdminList',
			  component: () => import('@/views/sys/admin')
		  },
		  {
			path:'/center',
			name:'CenterIndex',
			component: () => import('@/views/center')
		  },
		  {
			  path: '/role',
			  name: 'AdminRole',
			  component: () => import('@/views/sys/role')
		  },
		  {   path:'/resource',
			  name:'ResourceList',
			  component: () => import('@/views/sys/resource')
		  },
		  {   path:'/clue',
			  name:'ClueList',
			  component: () => import('@/views/clu/clue')
		  },
		  {   path:'/record',
			  name:'RecordList',
			  component: () => import('@/views/clu/record')
		  },
		  {   path:'/riskanalyse',
			  name:'RiskAnalyse',
			  component: () => import('@/views/analyse/riskanalyse')
		  },
		  {   path:'/mapview',
			name:'MapView',
			component: () => import('@/views/analyse/mapView')
		  },
		  {
			  path:'/analyse',
			  name:'StaticAnalyse',
			  component: () => import('@/views/analyse')
		  }
	  ]
    },
	{
		path:'/',
		name:'LoginIndex',
		component: () => import('@/views/login')
	},
	{
		path:'/register',
		name:'Register',
		component: () => import('@/views/login/register')
	},
	{
		path:'/password',
		name:'Password',
		component: () => import('@/views/login/password')
	}
  ]
})

router.beforeEach((to,from,next) => {
	const token = VueCookie.get('token')
	if(token){
		if(to.fullPath === '/'){
			next('/index')
		}else{
			next()
		}
	}else if(to.fullPath === '/' || to.fullPath === '/register' || to.fullPath === '/password'){
		next()
	}else{
		next('/')
	}
})

export default router
